package com.zggk.iroad.log.loglist;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zggk.iroad.R;
import com.zggk.iroad.adapter.recycleview.LGRecycleViewAdapter;
import com.zggk.iroad.adapter.recycleview.LGViewHolder;
import com.zggk.iroad.db.EntityManager;
import com.zggk.iroad.db.LogInfo;
import com.zggk.iroad.greendao.LogInfoDao;
import com.zggk.iroad.log.logadd.LogAddActivity;
import com.zggk.iroad.log.logdbmx.LogDbMxActivity;
import com.zggk.iroad.log.logmx.LogMxActivity;
import com.zggk.iroad.mvp.MVPBaseActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class LogListActivity extends MVPBaseActivity<LogListContract.View, LogListPresenter> implements LogListContract.View {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.month_txt)
    TextView monthTxt;
    @Bind(R.id.month_layout)
    RelativeLayout monthLayout;
    @Bind(R.id.log_rv)
    RecyclerView rcyhRecycle;
    @Bind(R.id.sm_lay)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.up_te)
    TextView upTe;
    private LogInfoDao logInfoDao;
    private List<LogInfo> Datas = new ArrayList();
    private LGRecycleViewAdapter<LogInfo> adapter;
    private int PAGEINDEX;

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initAdapter();
        initView();
        initData();
    }

    private void initAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(LogListActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rcyhRecycle.setLayoutManager(manager);
        rcyhRecycle.setHasFixedSize(true);
        rcyhRecycle.setNestedScrollingEnabled(false);
        adapter = new LGRecycleViewAdapter<LogInfo>(Datas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_log;
            }

            @Override
            public void convert(LGViewHolder holder, LogInfo zCDBean, int position) {


                holder.setText(R.id.gygs, zCDBean.getGYDW());
                if (zCDBean.getISDB().equals("1")) {
                    String time = zCDBean.getXCSJ().replace("/","-");
                    holder.setText(R.id.time,time.replace("2020",""));
                    holder.setText(R.id.type, "未上传");
                    holder.setTextColor(R.id.type, Color.parseColor("#CF1F1F"));
                } else if (zCDBean.getISDB().equals("0")) {
                    holder.setText(R.id.time,zCDBean.getXCSJ() );
                    holder.setText(R.id.type, "已上传");
                    holder.setTextColor(R.id.type, Color.parseColor("#548242"));
                }
            }
        };
        rcyhRecycle.setAdapter(adapter);
        adapter.setOnItemClickListener(R.id.item_layout, new LGRecycleViewAdapter.ItemClickListener() {
            @Override
            public void onItemClicked(View view, int position) {
                if (refreshLayout.isRefreshing() || refreshLayout.isLoading()) {
                    ToastUtils.showShort("等待数据加载完成再进行操作");
                } else {
                    if (Datas.get(position).getISDB().equals("1")) {//本地
                        Intent intent = new Intent(LogListActivity.this, LogDbMxActivity.class);
                        intent.putExtra("RZID", Datas.get(position).getId());
                        startActivityForResult(intent, 1);
                    } else {//网络
                        Intent intent = new Intent(LogListActivity.this, LogMxActivity.class);
                        intent.putExtra("RZID", Datas.get(position).getRZID());
                        startActivity(intent);
                    }
                }
            }
        });

    }

    private void initData() {
        refreshLayout.autoRefresh();
    }

    private void initView() {
        titleText.setText("巡查日志");
        logInfoDao = EntityManager.getInstance().getLogInfoDao();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (NetworkUtils.isAvailableByPing()) {//有网
                    PAGEINDEX = 1;
                    mPresenter.testinfo(monthTxt.getText().toString(), PAGEINDEX);
                } else {
                    Datas.clear();
                    Datas.addAll(logInfoDao.loadAll());
                    if (refreshLayout.isRefreshing()) {
                        refreshLayout.finishRefresh();
                    }
                }
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                PAGEINDEX ++;
                mPresenter.testinfo(monthTxt.getText().toString(), PAGEINDEX);
            }
        });
        monthTxt.setText(new SimpleDateFormat("yyyy年MM月").format(Calendar.getInstance().getTime()));
    }

    @Override
    protected int getLayout() {
        return R.layout.log_list;
    }

    @OnClick({R.id.back, R.id.up_te, R.id.add_te,R.id.month_txt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add_te:
                Intent intent = new Intent(LogListActivity.this, LogAddActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.month_txt:
                TimePickerView ksTimes = new TimePickerView.Builder(LogListActivity.this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        refreshLayout.autoRefresh();
                        monthTxt.setText(new SimpleDateFormat("yyyy年MM月").format(date));
                    }
                }).setType(new boolean[]{true, true, false, false, false, false})
                        .build();
                ksTimes.setDate(Calendar.getInstance());
                ksTimes.show();
                break;
            case R.id.up_te:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2) {
            if (requestCode == 1) {
                refreshLayout.autoRefresh();
            }
        }
    }

    @Override
    public void getData(LogListbean videoVos2) {
        if (refreshLayout.isLoadmoreFinished()) {
            refreshLayout.setLoadmoreFinished(false);
        }
        if (videoVos2.getRZDATA() != null && videoVos2.getRZDATA().size() > 0) {
            if (PAGEINDEX==1) {
                Datas.clear();
                Datas.addAll(logInfoDao.loadAll());
            }
            for (int i = 0; i < videoVos2.getRZDATA().size(); i++) {
                LogInfo logInfo = new LogInfo();
                logInfo.setXCSJ(videoVos2.getRZDATA().get(i).getXCSJ());
                logInfo.setGYDW(videoVos2.getRZDATA().get(i).getGYDW());
                logInfo.setRZID(videoVos2.getRZDATA().get(i).getRZID());
                logInfo.setISDB("0");
                Datas.add(logInfo);
            }
            adapter.notifyDataSetChanged();
            if (videoVos2.getRZDATA().size() < 25) {
                refreshLayout.setLoadmoreFinished(true);
            }
        } else {
            refreshLayout.setLoadmoreFinished(true);
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadmore();
        }
    }
}
